﻿using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.SceneSystem;
using MrPP.Restful;
using Proyecto26;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Shows_3D
{
    public class Login : MonoBehaviour
    {
        [SerializeField]
        private string _uri;
        [SerializeField]
        private LoginInputField _phoneNumber;
        [SerializeField]
        private LoginInputField _VerificationCodeField;

        private string phoneNum = null;
        private string verificationCode = null;

        private void verifyVerificationCode(Data data)
        {
            if (data.datas.ret)
            {
                Debug.Log(data.datas.msg);
            }
            else
            {
                Debug.LogError(data.datas.msg);
            }
        }

        private async Task loadScene()
        {
            IMixedRealitySceneSystem sceneSystem = MixedRealityToolkit.Instance.GetService<IMixedRealitySceneSystem>();
            await sceneSystem.LoadContent("Main", LoadSceneMode.Single);
        }
        /// <summary>
        /// 登录
        /// </summary>
        public void login()
        {
            verificationCode = _VerificationCodeField.text;

            //判断验证码输入是否正确
            RequestHelper options = new RequestHelper {Uri = _uri};
            options.Params.Add("c", "hololens");
            options.Params.Add("a", "check_captcha");
            options.Params.Add("phone", phoneNum);
            options.Params.Add("sms_captcha", verificationCode);

            RestfulManager.GetOrCreateInstance.post<Data>(options, (data) =>
            {
                _VerificationCodeField.data = data;
                if (data.datas.ret)
                {
                    //_ = loadScene();
                    //验证成功,进入单机模式
                    Control.Instance.PostToFsm("startStandAlone");
                }
            });
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        public void getVerificationCode()
        {
            phoneNum = _phoneNumber.text;
            if (string.IsNullOrEmpty(phoneNum))
                return;
            RequestHelper options = new RequestHelper();
            options.Method = "GET";
            options.Uri = _uri;
            options.Params.Add("c", "hololens");
            options.Params.Add("a", "getSMS");
            options.Params.Add("phone", phoneNum);
            options.Params.Add("type", "2");

            RestfulManager.GetOrCreateInstance.get<Data>(options, (data) =>
            {
                _phoneNumber.data = data;
                Debug.Log("msg is : " + data.datas.msg);
            });
        }
    }
}
